<?php
/**
 * Created by PhpStorm.
 * User: My
 * Date: 2018/10/15
 * Time: 14:02
 */

namespace App\Admin\Controllers;

use App;

use App\Models\TestingChannel;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ChannelController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.channel.header.title'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('backend.channel.header.title'));
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.channel.header.title'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(TestingChannel::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID')->sortable();
            $grid->code(trans('backend.channel.column.code'));
            $grid->name(trans('backend.channel.column.name'));
            $grid->desc(trans('backend.channel.column.desc'));
            $grid->page_type(trans('backend.channel.column.page_type'));
            $grid->config(trans('backend.channel.column.config'));
            $grid->status(trans('backend.channel.column.status'))->display(function ($status) {
                return $status == 0 ? '停止使用' : '可用';
            });
            $grid->add_time(trans('backend.channel.column.add_time'))->display(function ($addTime) {
                return date('Y-m-d H:i:s', $addTime);
            });

            $grid->disableExport();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(TestingChannel::class, function (Form $form) {
            $form->text('code', trans('backend.channel.column.code'))->rules('required');
            $form->text('name', trans('backend.channel.column.name'))->rules('required');
            $form->text('page_type', trans('backend.channel.column.page_type'))->rules('required');
            $form->textarea('config', trans('backend.channel.column.config'));
            $form->textarea('desc', trans('backend.channel.column.desc'));
            $form->select('status', trans('backend.channel.column.status'))->options(['0' => '停止使用', 1 => '可用'])->default(1);
        });

    }
}